﻿@model MenuRecordModel

@using SmartStore.Collections
@using SmartStore.Admin.Models.Menus

@if (Model.ItemTree.HasChildren)
{
	<div class="list-group list-group-flush">
		@foreach (var item in Model.ItemTree.Children)
		{
			@ItemGroup(item, 0, 42)
		}
	</div>
}
else
{
	<div class="text-muted px-3 py-3">
		@T("Admin.ContentManagement.Menus.NoMenuItemsAvailable")
	</div>
}

@helper ItemGroup(TreeNode<SmartStore.Web.Framework.UI.MenuItem> node, int level, int padding)
{
	var item = node.Value;
    var parentId = node.Parent != null ? node.Parent.Value.EntityId : 0;
	var indentStyle = " style=\"padding-left: {0}px;\"".FormatInvariant(padding);
    var rtl = WorkContext.WorkingLanguage.Rtl;

	<div class="list-group-item menu-item d-flex align-items-center py-2@(level == 0 ? "" : " sub-item")" data-id="@item.EntityId" data-parentid="@parentId">
		<div class="item-commands btn-group">
			<button type="button" class="btn btn-light btn-flat btn-no-border rounded-0 btn-item-menu dropdown-toggle no-chevron" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
				<i class="fa fa-ellipsis-v"></i>
			</button>
			<div class="dropdown-menu @(rtl ? "dropdown-menu-right" : "dropdown-menu-left")">
				<div class="dropdown-header h6 text-uppercase">
					@T("Admin.ContentManagement.AddMenuItem")
				</div>
				@foreach (var provider in Model.AllProviders)
				{
					<a class="dropdown-item" href="@Url.Action("CreateItem", new { providerName = provider.Value, menuId = Model.Id, parentItemId = item.EntityId, area = "Admin" })">
						<i class="fa fa-fw fa-plus"></i>
						<span>@provider.Text</span>
					</a>
				}
				<div class="dropdown-divider"></div>
				<a class="dropdown-item" href="@Url.Action("EditItem", new { id = item.EntityId, area = "Admin" })">
					<i class="far fa-fw fa-edit"></i>
					<span>@T("Common.Edit")</span>
				</a>
				<a class="menu-item-command dropdown-item@(node.Previous == null ? " disabled" : "")" href="#" data-action="move-item-up" data-url="@Url.Action("MoveItem", new { menuId = Model.Id, sourceId = item.EntityId, direction = "up", area = "Admin" })">
					<i class="fa fa-fw fa-angle-up"></i>
					<span>@T("Common.MoveUp")</span>
				</a>
				<a class="menu-item-command dropdown-item@(node.Next == null ? " disabled" : "")" href="#" data-action="move-item-down" data-url="@Url.Action("MoveItem", new { menuId = Model.Id, sourceId = item.EntityId, direction = "down", area = "Admin" })">
					<i class="fa fa-fw fa-angle-down"></i>
					<span>@T("Common.MoveDown")</span>
				</a>
				<div class="dropdown-divider"></div>
				<a class="menu-item-command dropdown-item" href="#" data-action="delete-item" data-url="@Url.Action("DeleteItem", new { id = item.EntityId, area = "Admin" })">
					<i class="far fa-fw fa-trash-alt"></i>
					<span>@T("Common.Delete")</span>
				</a>
			</div>
		</div>
		<div class="item-text" @Html.Raw(indentStyle)>
			@if (item.Icon.HasValue())
			{
				<i class="mr-1 fa-fw @item.Icon" title="@item.Summary"></i>
			}
			<a class="@(!item.Visible ? "muted" : "")" href="@Url.Action("EditItem", new { id = item.EntityId, area = "Admin" })">
				@if (item.Text.HasValue())
				{
					<span>@item.Text</span>
				}
				else
				{
					<span>(@T(item.ResKey.NullEmpty() ?? "Common.NoTitle"))</span>
				}
			</a>
		</div>
	</div>
	if (node.HasChildren)
	{
		<div class="list-group">
			@foreach (var child in node.Children)
			{
				@ItemGroup(child, level + 1, padding + 40);
			}
		</div>
	}
}
